package com.house.framework.controller;


import com.house.framework.entity.Logininfo;
import com.house.framework.query.LogininfoQuery;
import com.house.framework.service.ILogininfoService;
import com.house.framework.utils.AjaxResult;
import com.house.framework.utils.PageList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @ClassName LogininfoController
 * // @RequestMapping(value = "{id}", method = RequestMethod.GET) == @GetMapping("{id}")
 * @Version 1.0
 */
@RestController // ==@ResponseBody+@Controller
@RequestMapping("/logininfo")
@Api(tags = "登录接口",description = "登录接口详细描述")
public class LogininfoController {
    
    @Autowired
    private ILogininfoService logininfoService;
    
    /**
     *@Description 查询一个
     *@Param
     *@Return 
     */
    @ApiOperation(value = "登录查询一条",notes = "根据id查询")
    @GetMapping("{id}")
    public Logininfo queryById(@PathVariable("id") Long id) {
        
        return logininfoService.findById(id);
    }
    
    /**
     *@Description 查询所有
     *@Param
     *@Return 
     */
    @ApiOperation(value = "查询所有",notes = "查询所有登录信息")
    @GetMapping
    public List<Logininfo> queryAll() {
        return logininfoService.findAll();
    }
    
    /**
     *@Description 分页数据 页面发送请求，携带json格式
     *@Param
     *@Return
     * // @RequestBody：将请求体中json格式的参数，封装到query中
     */
    @ApiOperation(value = "分页查询",notes = "需要当前页和每页条数")
    @PostMapping
    public PageList<Logininfo> queryPage(@RequestBody LogininfoQuery query) {
        return logininfoService.queryList(query);
    }
    
    /**
     *@Description 删除
     *@Param
     *@Return 
     */
    @ApiOperation(value = "删除操作",notes = "根据id删除")
    @DeleteMapping("{id}")
    public AjaxResult deleteById(@PathVariable("id") Long id) {
        try {
            logininfoService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            // 链式编程 高级
            return AjaxResult.me().setMsg("系统繁忙，请稍后重试...");
        }
    }

    /**
     *@Description 添加修改
     *@Param
     *@Return
     */
    @ApiOperation(value = "登录添加或修改",notes = "如果有id就是修改否则就是添加")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Logininfo logininfo) {
        try {
            if (logininfo.getId() == null) {
                logininfoService.save(logininfo);
            } else {
                logininfoService.update(logininfo);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            // 链式编程 高级
            return AjaxResult.me().setMsg("系统繁忙，请稍后重试...");
        }
    }

    /**
     *@Description 批量删除
     *@Param
     *@Return
     */
    @ApiOperation(value = "批量删除",notes = "根据id删除")
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids) {
        try {
            logininfoService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            // 链式编程 高级
            return AjaxResult.me().setMsg("系统繁忙，请稍后重试...");
        }
    }
}
